﻿
using CMS.Database;
using CMS.IRepository;
using CMS.IService;
using CMS.Repository;
using CMS.Service;
using JT808.DotNetty.Abstractions;
using JT808.DotNetty.Core;
using JT808.DotNetty.Core.Handlers;
using JT808.DotNetty.SimpleServer.Handlers;
using JT808.DotNetty.Tcp;
using JT808.Protocol;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Threading.Tasks;

namespace JT808.DotNetty.SimpleServer
{
    class Program
    {
        static IConfigurationRoot conf;

        static async Task Main(string[] args)
        {
            var serverHostBuilder = new HostBuilder()
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                conf = config.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                                .Build();
            })
            .ConfigureLogging((context, logging) =>
            {
                logging.AddConsole();
                logging.SetMinimumLevel(LogLevel.Trace);
            })
            //配置依赖注入
            .ConfigureServices((hostContext, services) =>
            {
                services.AddSingleton<ILoggerFactory, LoggerFactory>();
                services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
               
                AddDBService(services, conf["sqlserver:ConnectionString"]);

                services.AddJT808Configure()
                        .AddJT808NettyCore(hostContext.Configuration)
                        .AddJT808TcpNettyHost()
                        // 自定义Tcp消息处理业务
                        .ReplaceMsgIdHandler<JT808MsgIdTcpSimpleHandler>()
                        .Builder();
            });
            
            await serverHostBuilder.RunConsoleAsync();
            

        }
        private static void AddDBService(IServiceCollection services, string connctionstring)
        {
            services.AddSingleton<IDapperDbContext>(_ => new MsSqlDbContext(connctionstring));
            services.AddScoped(typeof(JT808Serializer));
            services.AddScoped<IBaseGpsTyreSumRepository, BaseGpsTyreSumRepository>();
            services.AddScoped<IBaseTyreHistoricalRecordsRepository, BaseTyreHistoricalRecordsRepository>();

            services.AddScoped<IBaseConfigRepository, BaseConfigRepository>();
            services.AddScoped<IBaseConfigService, BaseConfigService>();

            services.AddScoped<ICollectTyresDetailRepository, CollectTyresDetailRepository>();
            services.AddScoped<ICollectTyresDetailService, CollectTyresDetailService>();

            services.AddScoped<ICollectMachineGpsService, CollectMachineGpsService>();
            services.AddScoped<ICollectMachineGpsRepository, CollectMachineGpsRepository>();

            services.AddScoped<IBaseGpsCarSumService, BaseGpsCarSumService>();
            services.AddScoped<IBaseGpsCarSumRepository, BaseGpsCarSumRepository>();

            services.AddScoped<ICommonService, CommonService>();
            services.AddScoped<ICommonRepository, CommonRepository>();

            services.AddScoped<IBaseCarTypeService, BaseCarTypeService>();
            services.AddScoped<IBaseCarTypeRepository, BaseCarTypeRepository>();

            services.AddScoped<IBaseCarTypeTyrePositionService, BaseCarTypeTyrePositionService>();
            services.AddScoped<IBaseCarTypeTyrePositionRepository, BaseCarTypeTyrePositionRepository>();

            services.AddScoped<IBaseCarService, BaseCarService>();
            services.AddScoped<IBaseCarRepository, BaseCarRepository>();

            services.AddScoped<IBaseDeviceService, BaseDeviceService>();
            services.AddScoped<IBaseDeviceRepository, BaseDeviceRepository>();

            services.AddScoped<IBaseTyreModelService, BaseTyreModelService>();
            services.AddScoped<IBaseTyreModelRepository, BaseTyreModelRepository>();

            services.AddScoped<IBaseTyreBrandService, BaseTyreBrandService>();
            services.AddScoped<IBaseTyreBrandRepository, BaseTyreBrandRepository>();

            services.AddScoped<IBaseTyreService, BaseTyreService>();
            services.AddScoped<IBaseTyreRepository, BaseTyreRepository>();

            services.AddScoped<IBaseCarTypeService, BaseCarTypeService>();
            services.AddScoped<IBaseCarTypeRepository, BaseCarTypeRepository>();

            services.AddScoped<IBaseCarGroupService, BaseCarGroupService>();
            services.AddScoped<IBaseCarGroupRepository, BaseCarGroupRepository>();

            services.AddScoped<IBaseCarQueueService, BaseCarQueueService>();
            services.AddScoped<IBaseCarQueueRepository, BaseCarQueueRepository>();



        }

    }
}
